--命运之轮 莫莱伊
local id = 3000055
local s = _G["c"..id]
function s.initial_effect(c)
	c:SetUniqueOnField(1,0,id)
	c:EnableReviveLimit()
	--特殊召唤条件
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetCode(EFFECT_SPSUMMON_PROC)
	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
	e1:SetRange(LOCATION_EXTRA)
	e1:SetCondition(s.spcon2)
	e1:SetOperation(s.spop2)
	c:RegisterEffect(e1)

	--支付代价
	local e3=Effect.CreateEffect(c)
	e3:SetType(EFFECT_TYPE_FIELD)
	e3:SetCode(EFFECT_ACTIVATE_COST)
	e3:SetRange(LOCATION_MZONE)
	e3:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e3:SetTargetRange(0,1)
	e3:SetCost(s.costchk)
	e3:SetOperation(s.costop)
	c:RegisterEffect(e3)
	--支付代价
	local e6=Effect.CreateEffect(c)
	e6:SetType(EFFECT_TYPE_FIELD)
	e6:SetCode(0x10000000+id)
	e6:SetRange(LOCATION_MZONE)
	e6:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e6:SetTargetRange(0,1)
	c:RegisterEffect(e6)

	--加入手卡
	local e10=Effect.CreateEffect(c)
	e10:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_F)
	e10:SetCode(EVENT_TO_HAND)
	e10:SetRange(LOCATION_MZONE)
	e10:SetCountLimit(1)
	e10:SetCondition(s.necon)
	e10:SetTarget(s.target)
	e10:SetOperation(s.negop)
	c:RegisterEffect(e10) 

	--Special summon this card from GY
	local e2=Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id,1))
	e2:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e2:SetType(EFFECT_TYPE_QUICK_O)
	e2:SetCode(EVENT_FREE_CHAIN)
	e2:SetRange(LOCATION_GRAVE)
	e2:SetCountLimit(1,id)
	e2:SetTarget(s.sptg)
	e2:SetOperation(s.spop)
	c:RegisterEffect(e2)


end
s.listed_series={0x930}
function s.spfilter1(c,tp)
	return c:IsSetCard(0x930) and c:IsType(TYPE_XYZ) and c:IsAbleToGrave()
end
function s.spfilter2(c,tp)
	return c:IsSetCard(0x930) and c:IsType(TYPE_FUSION) and c:IsAbleToGrave()
end
function s.spfilter3(c,tp)
	return c:IsSetCard(0x930) and c:IsType(TYPE_SYNCHRO) and c:IsAbleToGrave()
end
function s.sfilter(c)
   return c:IsAbleToGrave() and c:IsSetCard(0x930)
end
function s.spcon2(e,c)
	if c==nil then return true end
	local tp=c:GetControler()
	local ft=Duel.GetLocationCount(tp,LOCATION_MZONE)
	local g=Duel.GetMatchingGroup(s.sfilter,tp,LOCATION_ONFIELD,0,nil)
	return g:IsExists(s.spfilter1,1,nil,c,tp)
	and g:IsExists(s.spfilter2,1,nil,c,tp)
	and g:IsExists(s.spfilter3,1,nil,c,tp)
	and ft>0
end
function s.spop2(e,tp,eg,ep,ev,re,r,rp,c)
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOGRAVE)
		local g1=Duel.SelectMatchingCard(tp,s.spfilter1,tp,LOCATION_ONFIELD,0,1,1,nil)
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOGRAVE)
		local g2=Duel.SelectMatchingCard(tp,s.spfilter2,tp,LOCATION_ONFIELD,0,1,1,nil)
		g1:Merge(g2)
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOGRAVE)
		local g3=Duel.SelectMatchingCard(tp,s.spfilter3,tp,LOCATION_ONFIELD,0,1,1,nil)
		g1:Merge(g3)
		Duel.SendtoGrave(g1,REASON_COST)
end


function s.costchk(e,te_or_c,tp)
	local ct=Duel.GetFlagEffect(tp,id)
	return Duel.CheckLPCost(tp,ct*600)
end
function s.costop(e,tp,eg,ep,ev,re,r,rp)
	Duel.PayLPCost(tp,600)
end

function s.necon(e,tp,eg,ep,ev,re,r,rp)
	return Duel.GetCurrentPhase()~=PHASE_DRAW and eg:IsExists(Card.IsControler,1,nil,1-tp)
end
function s.target(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.GetFieldGroupCount(tp,0,LOCATION_HAND)>0 end
	Duel.SetOperationInfo(0,CATEGORY_REMOVE,nil,1,0,LOCATION_HAND)
end
function s.negop(e,tp,eg,ep,ev,re,r,rp)  
	 local g=Duel.GetMatchingGroup(nil,1-tp,LOCATION_HAND,0,nil)
	if #g>0 then
		Duel.Hint(HINT_SELECTMSG,1-tp,HINTMSG_REMOVE)
		local sg=g:Select(1-tp,1,1,nil)
		Duel.HintSelection(sg)
	  if  Duel.Remove(sg,POS_FACEDOWN,REASON_RULE)~=0 and Duel.SelectYesNo(tp,aux.Stringid(id,0)) then
	  Duel.Hint(HINT_SELECTMSG,1-tp,HINTMSG_DESTROY)
	  local g=Duel.SelectMatchingCard(tp,nil,tp,LOCATION_ONFIELD+LOCATION_HAND,0,1,1,nil)
	  Duel.Destroy(g,REASON_EFFECT)  
	  local e1=Effect.CreateEffect(e:GetHandler())
	 e1:SetType(EFFECT_TYPE_FIELD)
	 e1:SetCode(EFFECT_CANNOT_ATTACK_ANNOUNCE)
	 e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	 e1:SetReset(RESET_PHASE+PHASE_END)
	 e1:SetTargetRange(0,1)
	 Duel.RegisterEffect(e1,tp)
	  end
	end
end
function s.costfilter(c)
	return c:IsSetCard(0x930) and c:IsAbleToGrave()
end
function s.sptg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.GetLocationCount(tp,LOCATION_MZONE)>0
		and e:GetHandler():IsCanBeSpecialSummoned(e,0,tp,false,false) and Duel.IsExistingMatchingCard(s.costfilter,tp,LOCATION_ONFIELD+LOCATION_HAND,0,1,nil) end
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,e:GetHandler(),1,0,0)
end
function s.spop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local g=Duel.SelectMatchingCard(tp,s.costfilter,tp,LOCATION_ONFIELD+LOCATION_HAND,0,1,1,nil)
	if Duel.SendtoGrave(g,REASON_EFFECT)==0 then return end
	if c:IsRelateToEffect(e) then
		Duel.SpecialSummon(c,0,tp,tp,true,false,POS_FACEUP)
	end
end